﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Data;


namespace Installer
{
    [RunInstaller(true)]
    public partial class Installer1 : System.Configuration.Install.Installer
    {
        public Installer1()
        {
            InitializeComponent();
        }

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Install(IDictionary stateSaver)
        {
            try
            {
                base.Install(stateSaver);

                string connectionName = Context.Parameters["servName"].Replace("”", "");
                connectionName = connectionName.Replace("\\\\","\\");

                string sqlConnectionString = "Data Source=" + connectionName + ";Integrated security=SSPI;database=master";

                executeQuery(sqlConnectionString, SQLHelper.CREATE_DATABASE);
                executeQuery(sqlConnectionString, SQLHelper.EXECUTE_DB_SETUP);
            }
            catch(Exception ex)
            {
                // OOOOOPS Exception WTF. Hey we are mooving on. Just don't care
                MessageBox.Show(ex.ToString());
            }
        }


        private static void executeQuery(string sqlConnectionString, string str)
        {
            SqlConnection conn = new SqlConnection(sqlConnectionString);
            SqlCommand myCommand = new SqlCommand(str, conn);

            try
            {
                conn.Open();
                myCommand.ExecuteNonQuery();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
        }


        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Commit(IDictionary savedState)
        {
            base.Commit(savedState);
        }


        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Rollback(IDictionary savedState)
        {
            base.Rollback(savedState);
        }

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Uninstall(IDictionary savedState)
        {
            base.Uninstall(savedState);
        }
    }
}
